package com.fdb.efp.nls.api.rest;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.efp.nls.service.facade.CtrLoanContService;
import com.fdb.efp.nls.service.vo.CtrLoanContVO;
import com.fdb.efp.nls.service.facade.AccLoanService;
import com.fdb.efp.nls.service.vo.AccLoanVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.efp.esb.service.facade.MsLoanService;
import com.fdb.efp.esb.service.facade.RepaymentLoanService;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.nls.common.constant.NlsApplyInfoEnums;
import com.fdb.efp.nls.service.facade.NlsApplyInfoService;
import com.fdb.efp.nls.service.vo.NlsApplyInfoVO;

@RestController
@RequestMapping("/api")
public class NlsApplyInfoController extends AbstractController {

	@Autowired
	@Qualifier("nlsApplyInfoService")
	private NlsApplyInfoService nlsApplyInfoService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	@Autowired
	@Qualifier("msLoanService")
	private MsLoanService msLoanService;

	@Autowired
	@Qualifier("repaymentLoanService")
	private RepaymentLoanService repaymentLoanService;

	@Autowired
	@Qualifier("ctrLoanContService")
	private CtrLoanContService ctrLoanContService;

	@Autowired
	@Qualifier("accLoanService")
	private AccLoanService accLoanService;
	/**
	 * 列表数据查询
	 * 
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/nls/apply/infos", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<NlsApplyInfoVO>> queryNlsApplyInfoAll(@RequestBody NlsApplyInfoVO vo) throws Exception {
		List<NlsApplyInfoVO> outputVo = null;
		// 根据用户角色获取用户权限方案
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = nlsApplyInfoService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = nlsApplyInfoService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = nlsApplyInfoService.queryAllCurrDownOrg(vo);
			break;
		case CurrOwnerPrd:
			outputVo = nlsApplyInfoService.queryAllCurrOwnerPrd(vo);
			break;
		default:
			outputVo = nlsApplyInfoService.queryAllOwner(vo);
			break;
		}
		/**
		 * 字段展示处理
		 * 	  1、将贷款期限合成一个字段展示，贷款期限 = 贷款期限 + 期限类型；
		 * 	  2、申请时间 = 申请日期 + 申请时间	
		 */
		this.dealMessageShow(outputVo);
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/nls/apply/info/{applySeq}", method = RequestMethod.GET)
	public @ResponseBody ResponseData<NlsApplyInfoVO> queryByPk(@PathVariable("applySeq") String applySeq) {
		NlsApplyInfoVO inVo = new NlsApplyInfoVO();
		inVo.setApplySeq(applySeq);
		NlsApplyInfoVO outputVo = nlsApplyInfoService.queryByPk(inVo);
//		if ("1".equals(applySeq)) {
//			// 马上贷款审核通知
//			MsLoanReq msLoanReq = new MsLoanReq();
//			msLoanReq.setAudRslt("P");
//			msLoanReq.setTxnFlowNo("000001");
//			MsLoanResp msLoanResp = msLoanService.msLoan(msLoanReq);
//		} else if ("2".equals(applySeq)) {
//			// 周转贷贷后还款试算
//			RepaymentLoanReq repaymentLoanReq = new RepaymentLoanReq();
//			RepaymentLoanResp repaymentLoanResp = repaymentLoanService.repaymentLoanService(repaymentLoanReq);
//		}
		// else if("3".equals(applySeq)) {
		// //周转贷贷款回收
		// LoanRecoveryReq loanRecoveryReq = new LoanRecoveryReq();
		// LoanRecoveryResp loanRecoveryResp = loanRecoveryService.loanRecoveryService(loanRecoveryReq);
		// }else if("4".equals(applySeq)) {
		// //周转贷还款计划查询
		// RepaymentPlanReq repaymentPlanReq = new RepaymentPlanReq();
		// RepaymentPlanResp repaymentPlanResp = repaymentPlanService.repaymentPlanService(repaymentPlanReq);
		// }else if("5".equals(applySeq)) {
		// //联合贷文件下载通知
		// FileDownReq fileDownReq = new FileDownReq();
		// FileDownResp fileDownResp = fileDownService.fileDownService(fileDownReq);
		// }
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键删除信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/nls/apply/info", method = RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody NlsApplyInfoVO inNlsApplyInfoVo) {
		int outputVo = nlsApplyInfoService.deleteByPk(inNlsApplyInfoVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inNlsApplyInfoVo
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/nls/apply/info", method = RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody NlsApplyInfoVO inNlsApplyInfoVo) throws Exception {
		int outputVo = nlsApplyInfoService.updateByPk(inNlsApplyInfoVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * 
	 * @param inNlsApplyInfoVo
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/nls/apply/info", method = RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertNlsApplyInfo(@RequestBody NlsApplyInfoVO inNlsApplyInfoVo) throws Exception {
		int outputVo = nlsApplyInfoService.insertNlsApplyInfo(inNlsApplyInfoVo);
		return getResponseData(outputVo);
	}
	
	/**
	  * 通过客户编号查询放款申请信息
	  * @return
	  * @author qiuyf
	  * @since 2019年1月17日 下午3:37:22
	  * @version 1.0
	 */
	@RequestMapping(value = "/nlsApplyInfo/by", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<NlsApplyInfoVO>> getNlsApplyInfo(@RequestBody NlsApplyInfoVO inNlsApplyInfoVo) {
		List<NlsApplyInfoVO> outputVo = nlsApplyInfoService.queryListByConditions(inNlsApplyInfoVo);
		this.dealResultShow(outputVo);
		return getResponseData(outputVo);
	}
	 
	/**
	  * 页面字段展示处理
	  * @param outputVo
	  * @return
	  * @author qiuyf
	  * @since 2019年1月18日 上午9:53:15
	  * @version 1.0
	 */
	private void dealMessageShow(List<NlsApplyInfoVO> outputVo) {
		this.dealResultShow(outputVo);
		outputVo.forEach(item -> {
			//根据用信流水号查询合同表得到所需数据
			CtrLoanContVO ctrLoanContVO = Optional.ofNullable(ctrLoanContService.queryOneCtrData(item.getApplySeq())).orElseGet(CtrLoanContVO::new);
			item.setContNo(ctrLoanContVO.getContNo());
			item.setChargeoffBrId(ctrLoanContVO.getChargeoffBrId());
			item.setRepaymentMode(ctrLoanContVO.getRepaymentMode());
			item.setRulingIrY(null == ctrLoanContVO.getRulingIrY() ? BigDecimal.ZERO : ctrLoanContVO.getRulingIrY());
			item.setIrAdjustMode(ctrLoanContVO.getIrAdjustMode());
			item.setCalFloatingRate(null == ctrLoanContVO.getCalFloatingRate() ? BigDecimal.ZERO : ctrLoanContVO.getCalFloatingRate());
			item.setOverdueRate(null == ctrLoanContVO.getOverdueRate() ? BigDecimal.ZERO : ctrLoanContVO.getOverdueRate());
			item.setAssureMeansMain(ctrLoanContVO.getAssureMeansMain());

			//查询台账表得到所需数据
			AccLoanVO accLoanVO = Optional.ofNullable(accLoanService.queryOneAccLoanData(item.getApplySeq())).orElseGet(AccLoanVO::new);
			item.setLendType(accLoanVO.getLendType());
		});
	}

	private void dealResultShow(List<NlsApplyInfoVO> outputVo){
		outputVo.forEach(item -> {
			if(NlsApplyInfoEnums.TermType.TERM_TYPE_Y.getKey().equals(item.getLoanTermType())) {
				item.setLoanTerm(item.getLoanTerm() + NlsApplyInfoEnums.TermType.TERM_TYPE_Y.getValue());
			}
			if(NlsApplyInfoEnums.TermType.TERMTYPE_M.getKey().equals(item.getLoanTermType())) {
				item.setLoanTerm(item.getLoanTerm() + NlsApplyInfoEnums.TermType.TERMTYPE_M.getValue());
			}
			item.setApplyTime(item.getApplyDate() + " " + item.getApplyTime());
		});
	}
}
